<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Login extends CI_Controller {
	/**
	 * 加载登录页面
	 */
	public function index() {
		$this->load->view ( 'login' );
	}
	/**
	 * 进行登录
	 */
	public function tologin(){
		$id = $this->input->post ( 'id' );
		$pwd = $this->input->post ( 'pwd' );
		/* 验证登录操作 */
		if (! empty ( $id ) && ! empty ( $pwd )) {
			$re = $this->admin->isExistAdmin ( $id );
			if (! $re) {
				$this->_failure ();
			} else {
				$re = $this->admin->adminPwd ( $id, $pwd );
				if (! $re) {
					$this->_failure ();
				} else {
					$this->_success ( $id );
				}
			}
		} else {
			$this->_failure ();
		}
	}
	/**
	 * 登录成功
	 */
	private function _success($id) {
		$this->admin->login ( $id );
		$url = site_url ( 'main' );
		redirect ( $url );
		return true;
	}
	/**
	 * 登录失败
	 */
	private function _failure() {
		$url = site_url ( 'login' );
		redirect ( $url );
	}
}